package com.gitee.wsl.time.calendar

import com.gitee.wsl.time.plusDays
import com.gitee.wsl.time.timestamp
import com.gitee.wsl.time.toLocalDate
import kotlinx.datetime.LocalDate


class HolidayCalendar(baseCalendar: Calendar?=null) : AbstractCalendar(baseCalendar) {
    private val excludedDays = ArrayList<LocalDate>() //TreeSet<LocalDate>()

    fun addExcludedDays(days: Collection<LocalDate>) {
        excludedDays.addAll(days)
    }

    override fun isTimeIncluded(timeStamp: Long): Boolean {
        if (!super.isTimeIncluded(timeStamp)) {
            return false
        }

        val day:LocalDate = timeStamp.toLocalDate()

        return !(excludedDays.contains(day))
    }

    override fun getNextIncludedTime(timeStamp: Long): Long {
        var timeStamp = timeStamp
        val baseTime: Long = super.getNextIncludedTime(timeStamp)
        if ((baseTime > 0) && (baseTime > timeStamp)) {
            timeStamp = baseTime
        }

        var day: LocalDate = timeStamp.toLocalDate() //DateHelper.millisToDate(timeStamp)

        while (excludedDays.contains(day)) {
            day = day.plusDays(1)
        }

        return day.timestamp
    }
}
